package com.easycode.devtool.domain;

import com.easycode.devtool.util.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * Created by carter on 2017/2/7. Copyright © 2016 －2017 深圳布谷鸟信息技术有限公司
 */
public class TableModel {
    private String tableName = "";
    private String tableComment="";
    private List<FieldModel> fields = Lists.newLinkedList();



    public TableModel(boolean initField)
    {
        if(initField)
        {
            FieldModel fieldModel = new FieldModel();
            fieldModel.setFieldName("id");
            fieldModel.setFieldComment("主键");
            fieldModel.setFieldLength(20);
            fieldModel.setFieldType("bigint");

            fields.add(fieldModel);

            FieldModel fieldModel2 = new FieldModel();
            fieldModel2.setFieldName("create_at");
            fieldModel2.setFieldComment("数据添加时间");
            fieldModel2.setFieldLength(0);
            fieldModel2.setFieldType("datetime");
            fields.add(fieldModel2);

            FieldModel fieldModel4 = new FieldModel();
            fieldModel4.setFieldName("update_at");
            fieldModel4.setFieldComment("数据修改时间");
            fieldModel4.setFieldLength(0);
            fieldModel4.setFieldType("datetime");
            fields.add(fieldModel4);

            FieldModel fieldModel3 = new FieldModel();
            fieldModel3.setFieldName("creator");
            fieldModel3.setFieldComment("创建人");
            fieldModel3.setFieldLength(20);
            fieldModel3.setFieldType("varchar");
            fieldModel3.setFieldDefaultValue("");
            fields.add(fieldModel3);


            FieldModel fieldModel5 = new FieldModel();
            fieldModel5.setFieldName("creator_id");
            fieldModel5.setFieldComment("创建人id");
            fieldModel5.setFieldLength(20);
            fieldModel5.setFieldType("bigint");
            fieldModel5.setFieldDefaultValue("0");
            fields.add(fieldModel5);
        }

    }


    public TableModel() {
      this(true);

    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableComment() {
        return tableComment;
    }

    public void setTableComment(String tableComment) {
        this.tableComment = tableComment;
    }

    public List<FieldModel> getFields() {
        return fields;
    }

    public void setFields(List<FieldModel> fields) {
        this.fields = fields;
    }

    public boolean addField(FieldModel fieldModel) {
        //得到一个map，方便判断是否存在
        Map<String,FieldModel> fieldModelMap = Maps.newHashMap();
        for(FieldModel field: fields)
        {
            fieldModelMap.put(field.getFieldName(),field);
        }


        if(fieldModelMap.containsKey(fieldModel.getFieldName()))
        {
            return false;
        }else
        {//如果不含有该字段，则添加到字段列表中
            fields.add(fieldModel);
            return true;
        }
    }

    public String getCamelName()
    {
        StringBuffer stringBuffer = new StringBuffer();
        List<String> stringList = StringUtils.split2List(tableName, "_");

        for(int i=0;i<stringList.size();i++)
        {
            String tem = stringList.get(i);
            tem = tem.substring(0,1).toUpperCase()+tem.substring(1,tem.length()).toLowerCase();
            stringBuffer.append(tem);

        }

        return stringBuffer.toString();
    }


    public String getVarialCamelName()
    {
        StringBuffer stringBuffer = new StringBuffer();
        List<String> stringList = StringUtils.split2List(tableName, "_");

        for(int i=0;i<stringList.size();i++)
        {
            String tem = stringList.get(i);
            if(i >0)
            {
                tem = tem.substring(0,1).toUpperCase()+tem.substring(1,tem.length()).toLowerCase();
            }else {
                tem = tem.toLowerCase();
            }
            stringBuffer.append(tem);

        }

        return stringBuffer.toString();
    }


}
